﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceManager.DataAccessLayer
{
    public class DonviDbOperation:IDatabaseOperation<DONVI>
    {
        QLThietBiEntities devicemanager;
        #region IDatabaseOperation<T> Members

        public int SaveRecord(DONVI record)
        {
            try
            {
                using (devicemanager = new QLThietBiEntities())
                {
                    devicemanager.DONVIs.Add(record);
                    devicemanager.SaveChanges();
                }
                return (int)record.DonViID;
            }
            catch
            {
                return -1;
            }
        }

        public bool UpdateRecord(DONVI record)
        {
            using (devicemanager = new QLThietBiEntities())
            {
                devicemanager.THIETBIs.Find(record.DonViID);
                devicemanager.SaveChanges();
                return true;
            }
        }

        public List<DONVI> GetRecord(DONVI record)
        {
            devicemanager = new QLThietBiEntities();
            IQueryable<DONVI> custQuery = devicemanager.DONVIs.AsQueryable<DONVI>();
            if (!string.IsNullOrEmpty(record.TenDonVi))
                custQuery = custQuery.Where(c => c.TenDonVi.Contains(record.TenDonVi));
            return custQuery.ToList();
        }

        public bool DeleteRecord(DONVI record)
        {

            using (devicemanager = new QLThietBiEntities())
            {
                var cust = devicemanager.THIETBIs.FirstOrDefault(c => c.ThietBiID == record.DonViID);
                devicemanager.THIETBIs.Remove(cust);
                devicemanager.SaveChanges();
                return true;
            }
        }

        #endregion
    }
}
